﻿using JetBrains.Annotations;
using MyEartags.Enumerate;
using MyEartags.PairVariety;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace MyEartags.EarTag
{
    public class EaroverbitAppService : ApplicationService, IEaroverbitAppService
    {
        private readonly IRepository<Earoverbit, int> _earoverbitRepository;

        private readonly IRepository<Varieties, int> _varietyRepository;

        public EaroverbitAppService(IRepository<Earoverbit, int> earoverbitRepository, IRepository<Varieties, int> varietyRepository)
        {
            _earoverbitRepository = earoverbitRepository;
            _varietyRepository = varietyRepository;
        }

        /// <summary>
        /// 耳号添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<EarResult<EaroverbitDto>> CreateEaroverbit(EaroverbitDto dto)
        {
            Earoverbit earoverbit = new Earoverbit();

            earoverbit.StorageDate = dto.StorageDate = DateTime.Now;
            earoverbit.VarietyId = dto.VarietyId;
            earoverbit.OverbitName = dto.OverbitName;
            earoverbit.OverbitType = dto.OverbitType;
            earoverbit.BirthState = (BirthState)(dto.BirthState);
            earoverbit.UserState = (UseState)(dto.UserState);
            earoverbit.StoragedPeo = dto.StoragedPeo;

            var result = await _earoverbitRepository.InsertAsync(earoverbit);
            var res = ObjectMapper.Map<Earoverbit, EaroverbitDto>(result);

            if (res != null)
            {
                return new EarResult<EaroverbitDto>() { Code = 200, Message = "耳号添加成功！" };
            }
            else
            {
                return new EarResult<EaroverbitDto>() { Code = 500, Message = "耳号添加失败！" };
            }
        }

        /// <summary>
        /// 获取出生状态列表
        /// </summary>
        /// <returns></returns>
        public List<object> GetBirthStateInfoList()
        {
            List<object> list = new List<object>();

            foreach (var item in Enum.GetValues<BirthState>())
            {
                list.Add(new
                {
                    bname = item.ToString(),
                    bId = item
                });
            }

            return list.ToList();
        }

        public List<object> GetUseStateInfoList()
        {
            List<object> list = new List<object>();

            foreach (var item in Enum.GetValues<UseState>())
            {
                list.Add(new
                {
                    Uname = item.ToString(),
                    useId = item
                });
            }

            return list.ToList();
        }

        /// <summary>
        /// 获取耳号列表
        /// </summary>
        /// <returns></returns>
        public async Task<IQueryable<OverbitShowDto>> GetEaroverbits(string? overbitName, int varietyId, int birthId, int useStateId)
        {
            var list = (from a in (await _earoverbitRepository.GetListAsync())
                        join b in (await _varietyRepository.GetListAsync()) on a.VarietyId equals b.Id
                        select new OverbitShowDto
                        {
                            Id = a.Id,
                            OverbitName = a.OverbitName,
                            OverbitType = a.OverbitType,
                            VarietyId = a.VarietyId,
                            VarietiesName = b.VarietiesName,
                            BirthState = (int)a.BirthState,
                            StorageDate = a.StorageDate,
                            StoragedPeo = a.StoragedPeo,
                            UserState = (int)a.UserState
                        }).AsQueryable();

            if (!string.IsNullOrEmpty(overbitName))
            {
                list = list.Where(a => a.OverbitName.Contains(overbitName));
            }

            if (varietyId > 0)
            {
                list = list.Where(a => a.VarietyId == varietyId);
            }

            if (birthId > 0)
            {
                list = list.Where(a => a.BirthState == birthId);
            }

            if (useStateId > 0)
            {
                list = list.Where(a => a.UserState == useStateId);
            }
            return list;
        }

        /// <summary>
        /// 耳号删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteAsync(int eid)
        {
            await _earoverbitRepository.DeleteAsync(eid);
        }

        /// <summary>
        /// 获取耳号数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<EaroverbitDto> GetEaroverbitData(int eid)
        {
            var result = await _earoverbitRepository.GetAsync(eid);
            return ObjectMapper.Map<Earoverbit, EaroverbitDto>(result);
        }

        /// <summary>
        /// 修改耳号信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<EarResult<EaroverbitDto>> UpdateAsync(EaroverbitDto dto)
        {
            //Earoverbit earoverbit = new Earoverbit();
            //await _earoverbitRepository.UpdateAsync(earoverbit);
            //var result = ObjectMapper.Map<Earoverbit, EaroverbitDto>(earoverbit);

            var res = await _earoverbitRepository.FindAsync(dto.Id);
            if (res == null)
            {
                throw new UserFriendlyException("没有找到对应的用户");
            }
            ObjectMapper.Map(dto, res);
            var count = await _earoverbitRepository.UpdateAsync(res);
            var result = ObjectMapper.Map<Earoverbit, EaroverbitDto>(count);

            if (result != null)
            {
                return new EarResult<EaroverbitDto>() { Code = 200, Message = "耳号修改成功！" };
            }
            else
            {
                return new EarResult<EaroverbitDto>() { Code = 500, Message = "耳号修改失败！" };
            }

        }
    }
}